﻿using DevTeam;
using System.Collections.Generic;
using System.Data;

//Destex AF
namespace ConfExpert.Forms.ImportWizards.Stok
{
    public partial class Destex_AF_Tip1 : XITopCekiListesi
    {
        int YuklemeId;
        public Destex_AF_Tip1(int YuklemeId) :
            base(YuklemeId)
        {
            InitializeComponent();

            this.YuklemeId = YuklemeId;
            this.Init("Destex AF Tip1 Top Listesi", true);
            this.FileDone += DestexTip1_FileDone;
        }

        void DestexTip1_FileDone(DataSet ds, string FileName)
        {
            foreach (DataTable dt in ds.Tables)
            {
                DataRowCollection rows = dt.Rows;
                DataColumnCollection cols = dt.Columns;

                int orderLineStart = -1;
                int orderLineEnd = -1;

                for (int i = 0; i < rows.Count; i++)
                {
                    DataRow dr = rows[i];
                    if (dr[0].ToStr().ToUpper().Contains("PATT NO."))
                        orderLineStart = i + 1;

                    else if (
                        orderLineStart > -1 &&
                        orderLineEnd == -1 &&
                        i > orderLineStart &&

                        dr[0].ToStr().ToUpper().StartsWith("TOTAL")
                        )
                        orderLineEnd = i - 1;
                }

                Dictionary<string, int> indexes = new Dictionary<string, int>();

                DataRow dr0 = dt.Rows[orderLineStart - 1];
                for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                    switch (dr0[colIndex].ToString())
                    {
                        case "Patt No.": indexes.Add("Article", colIndex); break;
                        case "Bale No.": indexes.Add("VendorRollId", colIndex); break;
                        case "Lot": indexes.Add("LotNo", colIndex); break;
                        case "Total (Y)":
                        case "Total (M)": indexes.Add("Qty", colIndex); break;
                    }


                string patternNo = rows[orderLineStart][indexes["Article"]].ToString();
                for (int i = orderLineStart; i < orderLineEnd + 1; i++)
                {
                    DataRow dr = rows[i];

                    if (Article.Select("Article='" + patternNo + "'").Length == 0)
                    {
                        DataRow drArticle = Article.NewRow();
                        drArticle["Article"] = patternNo;
                        Article.Rows.Add(drArticle);
                    }

                    DataRow drRoll = Roll.NewRow();
                    if (indexes.ContainsKey("LotNo"))
                        drRoll["LotNo"] = dr[indexes["LotNo"]];
                    if (indexes.ContainsKey("VendorRollId"))
                        drRoll["VendorRollId"] = dr[indexes["VendorRollId"]];
                    drRoll["Article"] = patternNo;
                    drRoll["Qty"] = dr[indexes["Qty"]];
                    Roll.Rows.Add(drRoll);
                }
            }


            //TODO: peyderpey gelişte, yeni yapıda, ayrımın KALAN kısmını net olarak bilmek gerek... KST'de yeni satır açılıyor ama bunda öyle değil!

        }
    }
}
